<?php
// +----------------------------------------------------------------------
// | ThinkSNS
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.thinksns.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: nonant <nonant@thinksns.com>
// +----------------------------------------------------------------------
//

//记录
class RecordAction extends Action{
	
	function _initialize(){
		$this->setTitle( '我的记录 - 空间' );
	}	
	
	private function __show($strType){
		if($strType=='index'){
			$map['uid'] = $this->mid;
		}elseif ($strType=='attention'){
			$map['uid'] = array('in',X('Follow')->getFollowData($this->mid,'user'));
		}

		$time = getdate(time());
		$_GET['d'] = in_array($_GET['d'],array('today','weekday','month'))?$_GET['d']:'';
		if($_GET['d']=='today'){
			$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],$time['mday'],$time['year']));
		}elseif ($_GET['d']=='weekday'){
			$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],$time['mday']-7,$time['year']));
		}elseif ($_GET['d']=='month'){
			$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],1,$time['year']));
		}

		$pRecord = D('Record');
		
		//转换url [杨德升添加]
		$record  = $pRecord->getNewData($this->mid);
		$record	 = preg_replace('/((?:https?|mailto).*?)(\s|　|&nbsp;|<br|\'|\"|$)/', '<a href="\1" target="_blank">\1</a>\2', $record);

		$this->assign('record',$record);
		$list = $pRecord->getData($map,7);

		//转换url [杨德升添加]
		foreach($list['data'] as $k => $v) {
			$list['data'][$k]['content'] = preg_replace('/((?:https?|mailto).*?)(\s|　|&nbsp;|<br|\'|\"|$)/', '<a href="\1" target="_blank">\1</a>\2', $list['data'][$k]['content']);
		}

		$this->assign('strtype',$strType);
		$this->assign('list',$list['data']);
		$this->assign('page',$list['html']);
		$this->assign('d',$_GET['d']);
		$this->display('index');
	}

	//首页
	function index(){
		$this->__show('index');
	}

	function attention(){
		$this->__show('attention');
	}

	function all(){
		$this->__show('all');
	}

	function doadd(){
		$data['uid']     = $this->mid;
		$data['content'] = htmlspecialchars($_POST['content'], ENT_QUOTES);
		$data['cTime']   = time();
		
		if($id = D('Record')->add($data)){
			//自动识别url [杨德升添加]
			$title['content'] = preg_replace('/((?:https?|mailto).*?)(\s|　|&nbsp;|<br|\'|\"|$)/', '<a href="\1" target="_blank">\1</a>\2', $data['content']);
			
			x('Feed')->put('space_record',$title,'','','',$id);
			echo $title['content'];
//			echo $id;
		}
	}

	function docomment(){
		$intReplyUid = intval($_POST['replyuid']);
		$intFid      = intval($_POST['fid']);
		$strContent  = htmlspecialchars( $_POST['content'] , ENT_QUOTES );
		$data['type']    = 'record';
		$data['appid']   = $intFid;
		//$data['name']    = $this->uname;
		$data['uid']     = $this->mid;
		$data['comment'] = $strContent;
		$data['touid']   = $intReplyUid;
		$data['cTime']   = time();
		$id = M('Comment')->add($data);
		if($id){
			if($intReplyUid){
				$title['type'] = '记录';
				$body['comment'] = $strContent;
				x('Notify')->put('home_commentcomment',$intReplyUid,$title,$body);
			}else{
				$info = D('Record')->where('id='.$intFid)->field('content,uid')->find();
				$title['content']  = $info['content'];
				$body['comment'] = $strContent;
				x('Notify')->put('home_recordcomment',$info['uid'],$title,$body);
			}
		}
	}

	function delreply(){
		return M('comment')->where("id=".intval($_POST['id']))->delete();
	}

	function delrecord(){
		 M('comment')->where("fid=".intval($_POST['id']))->delete();
		 M('record')->where("id=".intval($_POST['id']))->delete();
		 echo '1';
	}
}
?>